package com.sgcc.pms.getway.handler;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.sgcc.pms.getway.utils.WriteResponse;
import com.sgcc.pms.tocs.common.result.WrapperedResult;

import reactor.core.publisher.Mono;

@Component
public class JsonServerAuthenticationFailureHandler implements ServerAuthenticationFailureHandler {

	@Override
	public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
		WrapperedResult<String> result = null;
		if (exception instanceof UsernameNotFoundException) {
			result = WrapperedResult.error("1");
		} else if (exception instanceof BadCredentialsException) {
			result = WrapperedResult.error("2");
		} else if (exception instanceof LockedException) {
			result = WrapperedResult.error("3");
		}
		return WriteResponse.write(webFilterExchange, result);
	}

}
